<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchList">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="机构名称">
              <j-select-depart
                v-model="queryParam.officeId"
                :trigger-change="true"
                customReturnField="id"
                :multi="false"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="年度">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                value-format="yyyy-MM-dd"
                v-model="queryParam.year"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="季度">
              <j-dict-select-tag
                style="width: 100%"
                v-model="queryParam.quarter"
                placeholder="请选择"
                dict-code="sys_quarter"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchList" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button
                type="primary"
                style="
                  padding: 0 10px;
                  margin-left: 10px;
                  background-color: #1ea489 !important;
                  border: 1px solid #1ea489 !important;
                "
                @click="mergeReports"
                >合并报表</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :row-selection="rowSelection"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="departName" slot-scope="departName">
          <div style="width: 100%; height: 35px; line-height: 35px; overflow-x: hidden; overflow-y: auto">
            {{ departName }}
          </div>
        </template>
        <span slot="actionName">
          <a-button size="small" @click="openCreatModel" v-has="'report:add'" type="primary" icon="plus">新增</a-button>
        </span>
        <span slot="create" slot-scope="text, record">
          {{ record.createByText || record.realName }}
        </span>
        <span slot="ifstate" slot-scope="text, record">
          {{ record.status_dictText || record.isMerge_dictText }}
        </span>
        <span slot="applystate" slot-scope="text, record">
          {{ record.applystate_dictText || record.state_dictText }}
        </span>
        <span slot="action" slot-scope="text, record">
          <span v-has="'report:detail'">
            <a @click="handleCheckData(record)">查看</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a @click="handleEditData(record)">编辑</a>
          </span>
          <span v-has="'report:export'">
            <a-divider type="vertical" />
            <a @click="handleDownload(record)">下载</a>
          </span>
          <span v-if="record.status == 2 || record.isMerge == 1">
            <a-divider type="vertical" />
            <a>已合并</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span v-has="'report:back'">
            <span v-if="record.applystate === 1 || record.state === 3">
              <a-divider type="vertical" />
              <a-popconfirm title="确定退回吗?" @confirm="() => handleSendBack(record)">
                <a>退回</a>
              </a-popconfirm>
            </span>
          </span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <creatReport ref="creatModel" @openModelView="handleAddData"></creatReport>
    <component v-bind:is="ModelView" ref="modalForm" @ok="updateProList"></component>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import store from '@/store/'
import ModelView10 from './ModelView/ModelView10'
import { httpAction, getAction } from '@/api/manage'
import moment from 'moment'
import 'moment/locale/zh-cn'
import columns from './indexColumns'
import creatReport from './creatReport'
import { getApi } from '@/utils/neiORwai'

export default {
  name: 'bbsb',
  mixins: [JeecgListMixin],
  components: {
    creatReport,
    ModelView10,
  },
  data() {
    return {
      queryParam: {},
      superParam: {
        type: '1',
        code: '10',
        sjType: '8',
      },
      // 表头
      columns: columns(this),
      url: {
        list: '/sjxxzhbb/sjAuditInfo/queryGZWOneReportInfo',
        downLoad: '/sjxxzhbb/sjsbb/exportNdzyqynbsjgzqktjInfo',
        merge: '/sjxxzhbb/sjAuditInfo/generateAuditReport',
        back: '/common/sjProcCommon/backReport',
      },
      yearPickShow: false,
      banEdit: true,
      title: '中央企业内部审计工作情况统计表',
      modelNum: '10',
      reportIds: [],
      departInfo: store.getters.departInfo,
    }
  },
  computed: {
    ModelView: function () {
      return 'ModelView' + this.modelNum
    },
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.reportIds = []
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          // selectedRows.map((item) => {
          //   this.reportIds.push(item.id)
          // })
          this.reportIds = [...selectedRowKeys]
        },
      }
    },
  },
  watch: {
    'superParam.code': {
      handler: function (val) {
        if (val) {
          this.searchList()
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 查询条件
    searchList() {
      this.loadData(1)
    },

    // 年份选择器 begin
    moment,
    // 得到年份选择器的值
    handlePanelChange(value) {
      this.yearPickShow = false
      this.queryParam.year = moment(value).format('YYYY')
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
      if(this.queryParam.year){
        this.queryParam.year = moment(this.queryParam.year.slice(0, 4)).format('YYYY')
      }
    },
    // 年份选择器 end

    // 新增弹窗
    openCreatModel() {
      this.$refs.creatModel.visible = true
    },

    // 操作按钮
    handleAddData: function (record) {
      this.$refs.modalForm.add(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    handleCheckData: function (record) {
      this.$refs.modalForm.check(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = true
      this.$refs.modalForm.disableSubmit = true
    },
    handleEditData: function (record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    handleDeleteData: function (record) {
      this.$refs.modalForm.delete(record, () => {
        this.searchList()
      })
    },
    handleDownload: function (record) {
      let params = {}
      getAction(
        this.url.downLoad,
        Object.assign({ id: record.id, type: this.queryParam.sjType, sjType: this.queryParam.sjType }, params)
      )
        .then((res) => {
          if (res.success && res.result) {
            this.$message.success('正在下载，请稍等！')
            // let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
            let url = `${getApi() || window._CONFIG['domianURL']}/sys/common/static${res.result}`
           
           window.open(url, '_blank')
          } else {
          }
        })
        .finally(() => {})
    },
    updateProList() {
      this.loadData()
    },
    banBtn(record) {
      if (record.state == 0 || record.state == 2) {
        return true
      } else {
        return false
      }
    },
    mergeReports() {
      this.loading = true
      getAction(this.url.merge, { ids: JSON.stringify(this.reportIds) })
        .then((res) => {
          this.loading = false
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {})
    },
    handleSendBack(record) {
      // 退回
      let url = this.url.back
      let type = this.superParam.sjType
      getAction(url, { type, proId: record.id, reason: '' }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>